package autoFile.core.strategy;

import autoFile.core.strategy.interfaces.CreateFileStrategyInterface;
import com.google.common.base.CaseFormat;
import org.apache.commons.lang.StringUtils;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * Created by IntelliJ IDEA.
 * User: liuliwei
 * Date: 2017-08-16
 * Time: 下午5:02
 * Description: Gas service 生成 代码
 */
public class CreateUserProxyFileByGasUserServiceStrategy extends AbstractStrategy implements CreateFileStrategyInterface {

    @Override
    public void createFile(Class clazz) throws IOException {
        String userProxyTemplate = getUserProxyTemplate();
        String userProxyMethodTemplate = getUserProxyMethodTemplate();
        String className = clazz.getSimpleName(); //类名称
        String classNameSmall = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL).convert(className); //类名称小写
        String classNameWithService = className; //类名称后面有Service
        String classNameWithServiceSmall = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL).convert(classNameWithService); //类名称小写后面有Service

        if (className.contains("Service")) {
            className = className.replaceFirst("Service", "");
            classNameSmall = classNameSmall.replaceFirst("Service", "");
        }
        String targetClassName = className + "Proxy";//生成文件名称

        StringBuilder allMethodCodes = new StringBuilder();
        for (Method method : getPublicMethod(clazz)) {
            String parameterTypeAndName = decorateParameterNames(getParameterTypeAndName(method, "AvatarEntity", "RpcResult"));
            String parameterNames = getParameterNames(method);
            String decorateParameterNames = getParameterNames(method,"rpcResult");
            decorateParameterNames = decorateParameterNames(decorateParameterNames);
            String rpcConstant = getGongRpcConstant(className, method);

            String methodCode = userProxyMethodTemplate.replace("classNameWithServiceSmall", classNameWithServiceSmall);
            methodCode = methodCode.replace("methodName", method.getName());
            methodCode = methodCode.replace("parameters", parameterTypeAndName);
            methodCode = methodCode.replace("targetClassName", targetClassName);
            methodCode = methodCode.replace("parameterNames", parameterNames);
            methodCode = methodCode.replace("decorateParameterNames", decorateParameterNames);
            methodCode = methodCode.replace("rpcConstant", rpcConstant);
            methodCode += "\t";
            allMethodCodes.append(methodCode);
//            createGongRpcConstants(className, method, gongRpcConstantsWriter);
        }
        String replaceContent = userProxyTemplate.replace("#", allMethodCodes);
        String replace$ = replaceContent.replace("$", targetClassName);
        String ClassName = replace$.replace("ClassName", classNameWithService);
        String result = ClassName.replace("className", classNameWithServiceSmall);

        String pathPre = getBasicPath() + "com/gamejelly/gong2/gas/proxy/";
        String filePath = pathPre + targetClassName + ".java";

        FileWriter sourceWriter = new FileWriter(filePath);
        System.out.println("生成" + filePath);
        sourceWriter.write(result);
        sourceWriter.close();

    }

    /**
     * gas
     * proxy模板
     *
     * @return
     * @throws IOException
     */
    private String getUserProxyTemplate() throws IOException {
//        return getTemplateByFullPath("/Users/liuliwei/autoFile/template/userProxyTemplate");
//        return getTemplateByFullPath(getTemplateBasicPath() + "autoFile/template/gas/classTemplate");
        return getTemplate("template/gas/classTemplate");

    }

    /**
     * 得到 gas  proxy 方法模板
     *
     * @return
     * @throws IOException
     */
    private String getUserProxyMethodTemplate() throws IOException {
//        return getTemplateByFullPath(getTemplateBasicPath() + "autoFile/template/gas/methodTemplate");
//        return getTemplateByFullPath("/Users/liuliwei/autoFile/template/gas/methodTemplate");
        return getTemplate("template/gas/methodTemplate");
    }

    private String decorateParameterNames(String parameterNames) {
        if (StringUtils.isNotEmpty(parameterNames)) {
            parameterNames=","+parameterNames;
        }
        return parameterNames;
    }

}
